package com.zklh.leetcode.top100;

import com.zklh.leetcode.tree.model.TreeNode;

/**
 * 将有序数组转为二叉搜索树算法实现
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/10/26
 */
public class Solution108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return sortedArrayToBST(nums, 0, nums.length-1);
    }


    private TreeNode sortedArrayToBST(int[] nums, int start , int end) {
        if (start > end) {
            return null;
        }
        int middle = (start + end) / 2;
        TreeNode node = new TreeNode(nums[middle]);
        node.left = sortedArrayToBST(nums, start, middle-1);
        node.right = sortedArrayToBST(nums, middle+1, end);
        return node;
    }
}